package com.sxhuayuan.parking.controller.member;

import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.config.InvoiceStatus;
import com.sxhuayuan.parking.config.PayStatus;
import com.sxhuayuan.parking.config.Platform;
import com.sxhuayuan.parking.entity.*;
import com.sxhuayuan.parking.exception.SystemException;
import com.sxhuayuan.parking.plugin.invoice.InvoicePlugin;
import com.sxhuayuan.parking.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 发票接口
 * 
 * @author helong
 * @create 2019-08-28 10:00
 */
@Controller("memberInvoiceController")
@RequestMapping("/member/invoice")
public class InvoiceController extends BaseController {

	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService parkingLotService;

	@Resource(name = "orderServiceImpl")
	OrderService orderService;

	@Resource(name = "memberServiceImpl")
	MemberService memberService;

	@Resource(name = "invoiceServiceImpl")
	InvoiceService service;
	@Resource(name = "pluginServiceImpl")
	PluginService pluginService;

	@PostMapping("/qrcode")
	@ResponseBody
	public RespMessage qrCodeUrl(@RequestParam List<String> ids, @RequestParam String parkingLotSn) {
		Member current = memberService.getCurrent();
		if (current == null) {
			return RespMessage.error(RespMessage.Status.INVALID_TOKEN, "登录超时");
		}
		ParkingLot parkingLot = parkingLotService.findBySn(parkingLotSn);
		if (parkingLot == null) {
			return RespMessage.error("车场不存在");
		}
		InvoiceSetting invoiceSetting = parkingLot.getInvoiceSetting();
		if (invoiceSetting==null) {
			return RespMessage.error("该车场未开通开具发票功能");
		}
		// 先查符合开具发票的资格的订单
		List<Order> orders = orderService.findByOrderSnAndMemberIdAndParkingLot(ids, current, parkingLot);
		if (orders.size() != ids.size()) {
			return RespMessage.error("所选订单中包含不能开具发票的订单");
		}
		InvoicePlugin invoicePlugin = pluginService.getInvoicePlugin(invoiceSetting.getPluginId());
		if (invoicePlugin == null) {
			return RespMessage.error("发票系统异常");
		}
		BigDecimal total = BigDecimal.ZERO;
		for (Order order : orders) {
			if (order.getInvoiceStatus() == InvoiceStatus.success || order.getInvoiceStatus() == InvoiceStatus.invoiceWait) {
				return RespMessage.error("所选订单中包含已开具发票或开票中的订单，无法开具发票");
			}
			if (order.getRefundMoney().compareTo(BigDecimal.ZERO) > 0) {
				return RespMessage.error("所选订单中包含已退款的订单，无法开具发票");
			}
			if (order.getPlatform() == null || (order.getPlatform() != Platform.weixin && order.getPlatform() != Platform.alipay)) {
				return RespMessage.error("所选订单中包含非电子支付方式，无法开具发票");
			}

			if (order.getPayStatus() != PayStatus.success) {
				return RespMessage.error("所选订单中包含未支付成功的订单，无法开具发票");
			}
			total = total.add(order.getMoney());
		}
		if (total.compareTo(BigDecimal.ZERO) <= 0) {
			return RespMessage.error("所选实缴金额为0,不能开具发票");
		}
		// 生成开票记录
		Invoice invoice = new Invoice(orders, parkingLot.getId(), total, invoiceSetting.getTaxRate(), current, invoiceSetting.getPluginId());
		try {
			String qrCode = invoicePlugin.getInvoiceQrCode(invoice, parkingLot);
			invoice.setQrCode(qrCode);
			service.save(invoice);
			return RespMessage.success((Object) qrCode);
		} catch (SystemException e) {
			e.printStackTrace();
			return RespMessage.error(e.getMessage());
		}
	}

	@PostMapping("/list")
	@ResponseBody
	public RespMessage list(@RequestBody Pageable pageable){
		Member current = memberService.getCurrent();
		Page<Invoice> page = service.findPage(pageable, current);
		return RespMessage.success(page);
	}
}
